﻿using IBatisNet.Common.Utilities;
using IBatisNet.DataMapper;
using IBatisNet.DataMapper.Configuration;
using System.Data;
using IBatisNet.DataMapper.MappedStatements;
using IBatisNet.DataMapper.Configuration.Statements;
using IBatisNet.Common;
using IBatisNet.DataMapper.Scope;
using System.Collections;
namespace Persistence
{
    public class MapperExtend
    {
        /// <summary>
        /// dbcommand construction
        /// </summary>
        /// <param name="sqlMap"></param>
        /// <param name="statementName"></param>
        /// <param name="paramObject"></param>
        /// <returns></returns>
        private static IDbCommand GetDbCommand(ISqlMapper sqlMap, string statementName, object paramObject)
        {
            IStatement statement = sqlMap.GetMappedStatement(statementName).Statement;
            IMappedStatement mapStatement = sqlMap.GetMappedStatement(statementName);
            ISqlMapSession session = new SqlMapSession(sqlMap);
            if (sqlMap.LocalSession != null)
            {
                session = sqlMap.LocalSession;
            }
            else
            {
                session = sqlMap.OpenConnection();
            }
            RequestScope request = statement.Sql.GetRequestScope(mapStatement, paramObject, session);
            mapStatement.PreparedCommand.Create(request, session, statement, paramObject);

            IDbCommand command = session.CreateCommand(CommandType.Text);
            command.CommandText = request.IDbCommand.CommandText;

            foreach (IDataParameter pa in request.IDbCommand.Parameters)
            {
                IDbDataParameter para = sqlMap.LocalSession.CreateDataParameter();
                para.ParameterName = pa.ParameterName;
                para.Value = pa.Value;
                command.Parameters.Add(para);
            }

            //mapper.LocalSession.CreateDataAdapter(command).Fill(ds);

            return command;
        }
        /// <summary>
        /// 查询DataTable
        /// </summary>
        /// <param name="sqlMap"></param>
        /// <param name="statementName"></param>
        /// <param name="paramObject"></param>
        /// <returns></returns>
        public static DataTable ExecuteQueryForDataTable(ISqlMapper sqlMap, string statementName, object paramObject)
        {
            DataSet ds = new DataSet();
            bool isSessionLocal = false;
            IDalSession session = sqlMap.LocalSession;
            if (session == null)
            {
                session = new SqlMapSession(sqlMap);
                session.OpenConnection();
                isSessionLocal = true;
            }
            IDbCommand cmd = GetDbCommand(sqlMap, statementName, paramObject);
            try
            {
                cmd.Connection = session.Connection;
                IDbDataAdapter adapter = session.CreateDataAdapter(cmd);
                adapter.Fill(ds);
            }
            finally
            {
                if (isSessionLocal)
                {
                    session.CloseConnection();
                }
            }
            return ds.Tables[0];
        }
        /// <summary>
        /// 查询DataTable带输出参数
        /// </summary>
        /// <param name="sqlMap"></param>
        /// <param name="statementName"></param>
        /// <param name="statementName"></param>
        /// <param name="htOutPutParameter"></param>
        /// <returns></returns>
        public static DataTable ExecuteQueryForDataTable(ISqlMapper sqlMap, string statementName, object paramObject, out Hashtable htOutPutParameter)
        {
            DataSet ds = new DataSet();
            bool isSessionLocal = false;
            IDalSession session = sqlMap.LocalSession;
            if (session == null)
            {
                session = new SqlMapSession(sqlMap);
                session.OpenConnection();
                isSessionLocal = true;
            }
            IDbCommand cmd = GetDbCommand(sqlMap, statementName, paramObject);
            try
            {
                cmd.Connection = session.Connection;
                IDbDataAdapter adapter = session.CreateDataAdapter(cmd);
                adapter.Fill(ds);
            }
            finally
            {
                if (isSessionLocal)
                {
                    session.CloseConnection();
                }
            }
            htOutPutParameter = new Hashtable();
            foreach (IDataParameter parameter in cmd.Parameters)
            {
                if (parameter.Direction == ParameterDirection.Output)
                {
                    htOutPutParameter[parameter.ParameterName] = parameter.Value;
                }
            }
            return ds.Tables[0];
        }
    }
}
